package work.file;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipInputStream;

/**
 * Description
 * Copyright © 启明星辰 版权所有
 *
 * @author shenminglong
 * @date 2024/1/15
 */
public class FileTest03 {

    public static void main(String[] args) throws Exception {
        FileTest03 main = new FileTest03();
        main.zip1();
        System.out.println("AAA");
    }



    public void test1() throws Exception {
        File file = new File("src/main/java/work/file/ccc.txt");
        InputStream is = new FileInputStream(file);
        byte[] bytes = new byte[is.available()];//定义容器
        is.read(bytes);//将流读入byte
        String str = new String(bytes, "utf-8");//将byte转为String

        File f = new File("src/main/java/work/file/ddd.txt");
        OutputStream out = new FileOutputStream(f);
        out.write(str.getBytes(StandardCharsets.UTF_8));
    }

    public void test0() throws Exception {
        File file = new File("src/main/java/work/file/0000555005.zip");
        InputStream is = new FileInputStream(file);
        byte[] bytes = new byte[is.available()];//定义容器
        is.read(bytes);//将流读入byte
        String str = new String(bytes, "utf-8");//将byte转为String

        File f = new File("src/main/java/work/file/0000555006.zip");
        OutputStream out = new FileOutputStream(f);
        out.write(str.getBytes(StandardCharsets.UTF_8));
    }

    public void test2() throws Exception {
        File file = new File("src/main/java/work/file/0000555005.zip");
        FileInputStream is = new FileInputStream(file);
        byte[] bytes = new byte[is.available()];//定义容器
        is.read(bytes);//将流读入byte
        String str = new String(bytes, "utf-8");//将byte转为String

//        File f = new File("src/main/java/work/file/0000555006.zip");
        getFileByBytes(bytes, "src/main/java/work/file/a", "bbb.zip");
        System.out.println("AAA");
    }

    void md() throws Exception {
        File file1 = new File("src/main/java/work/file/0000555005.zip");
        File file2 = new File("src/main/java/work/file/a/0000555005.zip");

        System.out.println(this.getFileStr(file1).equals(this.getFileStr(file2)));
    }

    public String getFileStr(File file) throws Exception {
        FileInputStream is = new FileInputStream(file);
        byte[] bytes = new byte[is.available()];//定义容器
        is.read(bytes);//将流读入byte
        String str = new String(bytes, "utf-8");//将byte转为String
        return str;
    }

    private static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            File file = new File(filePath + File.separator + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (IOException e) {
            System.out.println("exception:{}" + e.getMessage());
        } finally {
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    System.out.println("exception:{}" + e.getMessage());
                }
            }
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    System.out.println("exception:{}" + e.getMessage());
                }
            }
        }
    }

    void encode() {
        String a = "伦家是一个非常长的字符串";
        //5Lym5a625piv5LiA5Liq6Z2e5bi46ZW/55qE5a2X56ym5Liy
        String encode = Base64.encode(a);
        System.out.println(encode);
        String decodeStr = Base64.decodeStr(encode);
        System.out.println(decodeStr);

        // md5加密
        String PASSWORD = "123456";
        String res1 = SecureUtil.md5(PASSWORD);
        String res2 = SecureUtil.md5(PASSWORD);
        System.out.println(res1);
        System.out.println(res2);
        System.out.println(res1.equals(res2));
        System.out.println("AAA");
    }

    public void zip() throws Exception {
        File file = FileUtil.newFile("E:\\idea_workspace\\maven_demo\\src\\main\\java\\work\\file\\demo\\a\\ttt");
        file.createNewFile();
        FileUtil.writeUtf8String("1234678", file);

        File file1 = FileUtil.newFile("E:\\idea_workspace\\maven_demo\\src\\main\\java\\work\\file\\demo\\a11");
        FileUtil.mkdir(file1);
        File file123 = ZipUtil.zip("E:\\idea_workspace\\maven_demo\\src\\main\\java\\work\\file\\demo");

        file1.delete();
    }

    public void zip1() throws Exception {

        String rootDirPath = "rootFile";
        File rootDir = FileUtil.newFile(rootDirPath);
        FileUtil.mkdir(rootDir);

        String dumpDirPath = rootDirPath + FileUtil.FILE_SEPARATOR + "dump";
        File dumpDir = FileUtil.newFile(dumpDirPath);
        FileUtil.mkdir(dumpDir);

        String indexDirPath = dumpDirPath + FileUtil.FILE_SEPARATOR + "index";
        File indexDir = FileUtil.newFile(indexDirPath);
        FileUtil.mkdir(indexDir);

        String fullDirPath = dumpDirPath + FileUtil.FILE_SEPARATOR + "full";
        File fullDir = FileUtil.newFile(fullDirPath);
        FileUtil.mkdir(fullDir);


        File indexFile = FileUtil.newFile(indexDirPath + FileUtil.FILE_SEPARATOR + "config_index.0000555005");
        indexFile.createNewFile();
        FileUtil.writeUtf8String("1234678", indexFile);

        File fullFile = FileUtil.newFile(fullDirPath + FileUtil.FILE_SEPARATOR + "mtxrule_1001");
        fullFile.createNewFile();
        FileUtil.writeUtf8String("AAAAA", fullFile);

        String path = FileUtil.getAbsolutePath(rootDir);

        File zipFile = ZipUtil.zip(path);
        FileUtil.del(path);
//        FileUtil.del(zipFile);


    }
}
